package com.aspire.boc.mms.wmf.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 公关方法：读取配置文件
 * 
 * @author liuyanyan
 * @version v0.1
 * @createDate 20121210
 * 
 */
public class ResourceManager {

	private static Properties prop = null;

	private static ResourceManager rm = null;

	Log logger = LogFactory.getLog(ResourceManager.class);

	private ResourceManager() {
		prop = new Properties();
		InputStream is = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("resource.properties");
		if (null == is)
			logger
					.warn("Not found resource file resource.properties in classpath!");
		else {
			try {
				prop.load(is);
			} catch (IOException ex) {
				logger.error("IOException", ex);
			}
		}
	}

	public static ResourceManager getInstance() {
		if (null == rm)
			rm = new ResourceManager();

		return rm;
	}

	public String getValue(String key) {
		String value = null;
		value = prop.getProperty(key);
		if (null != value)
			return value.trim();
		else
			return value;
	}

	public int getIntValue(String key) {
		String value = null;
		value = prop.getProperty(key);
		int retValue = -100000;

		if (null != value) {
			value = value.trim();
			if (value != null && value.length() > 0) {
				try {
					retValue = Integer.parseInt(value);
				} catch (NumberFormatException ex) {
					logger.error("NumberFormatException", ex);
				}
			}
		}
		return retValue;
	}

	public long getLongValue(String key) {
		String value = null;
		value = prop.getProperty(key);
		long retValue = -10000;
		if (null != value) {
			value = value.trim();
			if (value != null && value.length() > 0) {
				try {
					retValue = Long.parseLong(value);
				} catch (NumberFormatException ex) {
					logger.error("NumberFormatException", ex);
				}
			}
		}

		return retValue;
	}

}
